package com.sugon.controller;

import cn.hutool.core.collection.CollectionUtil;
import com.sugon.annotation.AutoLog;
import com.sugon.entity.MaterialBaseEntity;
import com.sugon.entity.MaterialRecordEntity;
import com.sugon.modules.sparepart.consts.MaterialBaseProgress;
import com.sugon.service.MaterialBaseService;
import com.sugon.service.MaterialRecordService;
import com.sugon.utils.MapUtil;
import com.sugon.utils.PageUtils;
import com.sugon.utils.Query;
import com.sugon.utils.R;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 备件领料单Controller
 *
 * @author YuXD
 * @date 2020-07-30 08:53:20
 */
@Controller
@RequestMapping("materialbase")
public class MaterialBaseController {
    @Autowired
    private MaterialBaseService materialBaseService;
    @Autowired
    private MaterialRecordService materialRecordService;

    /**
     * 查看列表
     */
    @RequestMapping("/list")
//    @RequiresPermissions("materialbase:list")
    @ResponseBody
    public R list(@RequestParam Map<String, Object> params) {
        //查询列表数据
        Query query = new Query(params);

        List<MaterialBaseEntity> materialBaseList = materialBaseService.queryList(query);
        int total = materialBaseService.queryTotal(query);

        PageUtils pageUtil = new PageUtils(materialBaseList, total, query.getLimit(), query.getPage());

        return R.ok().put("page", pageUtil);
    }

    /**
     * 查看信息
     */
    @RequestMapping("/info/{id}")
//    @RequiresPermissions("materialbase:info")
    @ResponseBody
    public R info(@PathVariable("id") String id) {
        MaterialBaseEntity materialBase = materialBaseService.queryObject(id);

        return R.ok().put("materialBase", materialBase);
    }

    /**
     * 查看信息
     */
    @RequestMapping("/infoDetail/{id}")
    @ResponseBody
    public R infoDetail(@PathVariable("id") String id) {
        MaterialBaseEntity materialBase = materialBaseService.queryObjectDetail(id);
        return R.ok().put("materialBase", materialBase);
    }

    /**
     * 审核处理
     */
    @RequestMapping("/check")
    @RequiresPermissions("materialbase:check")
    @ResponseBody
    public R check(@RequestParam Map<String, Object> params) {

        // 获取领料单ID
        String id = params.get("id") + "";
        // 校验下是否已添加备件
        List<MaterialRecordEntity> materialRecords = materialRecordService.queryByMater(id);
        if (CollectionUtil.isEmpty(materialRecords)) {
            return R.error("请在领料单添加备件后再进行审核!");
        }
        String checkRemark = cn.hutool.core.map.MapUtil.getStr(params, "checkRemark");
        ;
        int progress = MapUtil.getInt(params, "progress", MaterialBaseProgress.WAIT_APPROVE);
        MaterialBaseEntity materialBase = new MaterialBaseEntity();
        materialBase.setId(id);
        materialBase.setCheckRemark(checkRemark);
        materialBase.setIsCheck(progress);
        materialBase.setProgress(progress);
        materialBaseService.check(materialBase);
        return R.ok();
    }

    /**
     * 备件出库
     *
     * @param id
     * @return
     */
    @RequestMapping("/out/{id}")
    @RequiresPermissions("materialbase:out")
    @ResponseBody
    public R stockOut(@PathVariable("id") String id) {
        // 校验领料单是否存在
        MaterialBaseEntity materialBase = materialBaseService.queryObject(id);
        if (materialBase == null) {
            return R.error("领料单不存在！");
        }
        // 校验是否可出库
        if (materialBase.getProgress() != MaterialBaseProgress.WAIT_OUTBOUND) {
            return R.error("只有处于待出库状态的领料单才可以执行出库操作！");
        }
        return materialBaseService.stockOut(materialBase) > 0 ? R.error("出库错误") : R.ok();
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("materialbase:save")
    @ResponseBody
    public R save(@RequestBody MaterialBaseEntity materialBase) {
//        String jobCode = materialBase.getJobCode();
//        if (StrUtil.isEmpty(jobCode)) {
//            return R.error("非法维修工单号");
//        }
//        // 一个维修工单只可以关联一个领料单
//        MaterialBaseEntity materialBaseInDB = materialBaseService.queryByJobCode(jobCode);
//        if (materialBaseInDB != null) {
//            return R.error("一个维修工单只可以关联一个领料单");
//        }
        return materialBaseService.save(materialBase) > 0 ? R.ok() : R.error();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("materialbase:update")
    @ResponseBody
    public R update(@RequestBody MaterialBaseEntity materialBase) {
        materialBaseService.update(materialBase);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("materialbase:delete")
    @ResponseBody
    public R delete(@RequestBody String[] ids) {
        materialBaseService.deleteBatch(ids);

        return R.ok();
    }

    /**
     * 查看所有列表
     */
    @RequestMapping("/queryAll")
    @ResponseBody
    public R queryAll(@RequestParam Map<String, Object> params) {

        List<MaterialBaseEntity> list = materialBaseService.queryList(params);

        return R.ok().put("list", list);
    }
}
